package com.cci.kangdao.utilTool;


import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据库字段中有引号的转义成&quot;
 *
 * @author baiyunjiao
 */
public class StringToool {
    public static String tooo(String str) {
        str = str.replaceAll("\"", "&quot;");
        return str;
    }


    public static String checkExcel(String value, String content) {
        String UploadMsg = "";
        if ("单选题".equals(value) || "多选题".equals(value) || "是非题".equals(value)) {
            if (value.contains("单") || value.contains("多")) {
                if (content.contains("A.") && content.contains("B.") && content.contains("C.") && content.contains("D.")) {
                    if (content.contains("；B.") || content.contains("；C.") || content.contains("；D.") || content.contains("；E.") || content.contains("；F.") || content.contains("；G.")
                            || content.contains("；H.") || content.contains("；I.") || content.contains("；J.") || content.contains("；K.") || content.contains("；L.") || content.contains("；M.")
                            || content.contains("；N.") || content.contains("；O.") || content.contains("；P.") || content.contains("；Q.") || content.contains("；R.") || content.contains("；S.")
                            || content.contains("；T.") || content.contains("；U.") || content.contains("；V.") || content.contains("；W.") || content.contains("；X.") || content.contains("；Y.") || content.contains("；Z.")) {
                        UploadMsg = "上传失败，分隔符必须为英文分隔符";
                    } else {
                        if (content.contains(";")) {
                            if (content.split(";").length != 4) {
                                UploadMsg = "上传失败，分隔符必须有三个";
                            }
                        } else {
                            UploadMsg = "上传失败，分隔符必须为英文分隔符";
                        }
                    }
                } else {
                    UploadMsg = "上传失败，选项中必须包含A、B、C、D 四个选项,请检查";
                }
            } else if (value.contains("是")) {
                if (content.contains(";")) {
                    if (content.split(";").length != 2) {
                        UploadMsg = "上传失败，是非题必须有两个选项";
                    }
                } else {
                    if (!content.contains("是") || !content.contains("否")) {
                        UploadMsg = "上传失败，是非题必须有两个选项";
                    } else {
                        UploadMsg = "上传失败，分隔符必须为英文分隔符";
                    }
                }
            }
        } else {
            UploadMsg = "上传失败，考题类型必须是(单选题，多选题，是非题)";
        }
        return UploadMsg.trim();
    }

    /**
     * @description: 16进制转字符串
     * @param: null
     * @return:
     * @exception:
     * @author: wunan
     * @company: Persagy Technology Co.,Ltd
     * @since: 2021/7/26 16:27
     * @version: V1.0
     */
    public static String hexToStr(String hexStr) {
        if (hexStr == null || hexStr.equals("")) {
            return null;
        }
        hexStr = hexStr.replace(" ", "");
        byte[] baKeyword = new byte[hexStr.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            hexStr = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return hexStr;
    }

    public static List<String> getStringList(String arr, String split) {
        List<String> list = null;
        if (StringUtils.isNotBlank(arr)) {
            String[] splitList = arr.split(split);
            list = Arrays.stream(splitList).collect(Collectors.toList());
        }
        return list;
    }

    public static String getCellString(Cell cell) {
        if (null == cell || cell.toString().trim().equals("")) {
            return "";
        }
        return cell.getStringCellValue();
    }
}
